package fr.inserm.umr915.vcfserver.bio.ucsc.snp;

import fr.inserm.umr915.vcfserver.bio.Chromosomes;
import fr.inserm.umr915.vcfserver.util.ColumnSerializer;
import fr.inserm.umr915.vcfserver.xml.XmlStreamSerializer;

public class Snp
{
private String chrom;
private int chromStart;
private int chromEnd;
private String name;
private Float avHet;

public Snp()
   {
   }

public String getChrom()
   {
   return this.chrom;
   }
public int getChromStart()
   {
   return this.chromStart;
   }
public int getChromEnd()
   {
   return this.chromEnd;
   }
public String getName()
   {
   return this.name;
   }


public Float getAvHet()
   {
   return this.avHet;
   }

@Override
public String toString()
   {
   return this.getClass().toString() +  " 'chrom'= " + this.chrom +  " 'chromStart'= " + this.chromStart +  " 'chromEnd'= " + this.chromEnd +  " 'name'= " + this.name +  " 'avHet'= " + this.avHet ;
   }

/** XMLStreamWriter serialisation */
public static class StaxSerializer
	implements XmlStreamSerializer<Snp>
   {
   private String name;
   public StaxSerializer(String name)
	   {
	   this.name=name;
	   }
   @Override
   public void write(Snp o,javax.xml.stream.XMLStreamWriter w) throws javax.xml.stream.XMLStreamException
   	   {
	   w.writeStartElement(this.name);
	
	   w.writeStartElement("chrom");
	   w.writeCharacters(String.valueOf(o.getChrom()));
	   w.writeEndElement();
	   w.writeStartElement("chromStart");
	   w.writeCharacters(String.valueOf(o.getChromStart()));
	   w.writeEndElement();
	   w.writeStartElement("chromEnd");
	   w.writeCharacters(String.valueOf(o.getChromEnd()));
	   w.writeEndElement();
	   w.writeStartElement("name");
	   w.writeCharacters(String.valueOf(o.getName()));
	   w.writeEndElement();
	
	   if(o.avHet!=null)
		   {
		   w.writeStartElement("avHet");
		   w.writeCharacters(String.valueOf(o.getAvHet()));
		   w.writeEndElement();
		   }
	   w.writeEndElement();
	   }
   }

 public static Snp parse(String tokens[])
  {
  Snp o=new Snp();
  
  o.chrom=tokens[1];
  o.chromStart=Integer.parseInt(tokens[2]);
  o.chromEnd=Integer.parseInt(tokens[3]);
  o.name=tokens[4];

  o.avHet=Float.parseFloat(tokens[13]);
  if(o.avHet==0f) o.avHet=null;
  return o;
  }

public static class DelimitedOutputSerializer implements ColumnSerializer<Snp>
	{
	private String version;
	DelimitedOutputSerializer(String version)
		{
		this.version=version;
		}
	@Override
	public String[] getHeaders()
		{
		return new String[]{
			this.version+".name",
			this.version+".chrom",
			this.version+".chromStart",
			this.version+".chromEnd",
			this.version+".avHet"
			};
		}
	@Override
	public Object[] getColumns(Snp o)
		{
		return new Object[]{
			o.name,
			o.chrom,
			o.chromStart,
			o.chromEnd,
			o.avHet
			};
		}
	}
 
 
public static class Binding extends com.sleepycat.bind.tuple.TupleBinding<Snp>
   {
	private Chromosomes chromosomes;
   public Binding(Chromosomes chromosomes)
		{
	   	this.chromosomes=chromosomes;
		}
	@Override
   public Snp entryToObject(com.sleepycat.bind.tuple.TupleInput in)
      {
      Snp o=new Snp();
		o.chrom=this.chromosomes.getNameById(in.readByte());
		o.chromStart=in.readInt();
		o.chromEnd=in.readInt();
		int rsid=in.readInt();
		o.name=(rsid<1?null:"rs"+rsid);
		o.avHet=in.readFloat();
		if(o.avHet==0f) o.avHet=null;
      return o;
      }
	@Override
   public void objectToEntry(Snp o,com.sleepycat.bind.tuple.TupleOutput out)
      {
		out.writeByte(this.chromosomes.getIdByName(o.chrom));
		out.writeInt(o.chromStart);
		out.writeInt(o.chromEnd);
		out.writeInt(o.name!=null && o.name.startsWith("rs")?Integer.parseInt(o.name.substring(2)):-1);
		out.writeFloat(o.avHet==null?0f:o.avHet);
      }
   }
}
